package controllers;

import play.*;
import play.data.validation.Required;
import play.db.jpa.JPABase;
import play.mvc.*;

import java.util.*;

import models.*;

public class Application extends Controller {

	public static void index() {
		ArrayList<News> NewsList = new ArrayList<News>();
		NewsList = (ArrayList) News.findAll();

		if (session == null || session.get("username") == null) {
			try {
				Secure.logout();
			} catch (Throwable e) {

				return;
			}
		}
		if (!session.get("username").equalsIgnoreCase("admin")) {

			List<User> userlist = User.findAll();
			for (int i = 0; i < userlist.size(); i++) {
				if (userlist.get(i).UserName.equals(session.get("username"))) {
					User x = userlist.get(i);
					NormalUser(x);

				}
			}
		}

		else {
			render(NewsList);
		}
	}

	public static void NormalUser(User x) {
		render(x);

	}

	public static void login(String UserName, String Password)

	{

		ArrayList<User> userslist = (ArrayList) User.findAll();
		for (int i = 0; i < userslist.size(); i++) {
			User x = userslist.get(i);
			if (!x.UserName.equalsIgnoreCase("admin")) {
				if (x.UserName.equalsIgnoreCase(UserName)) {
					if (x.Password.equals(Password)) {
						render(x);
					}
				}
			} else {
				if (x.Password.equals(Password)) {
					loginadmin(x);
				}

			}
		}

	}

	public static void viewNews(long id) {
		News news = News.findById(id);
		render(news);
	}

	public static void loginadmin(User x) {
		render(x);
	}
}